package com.pmsystem.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.OfficeMapper;
import com.pmsystem.pojo.Office;
import com.pmsystem.pojo.OfficeExample;
import com.pmsystem.pojo.OfficeExample.Criteria;

/**
 * 
* <p>Title: OfficeService</p>  
* <p>Description:处理办公室信息的服务层 </p>  
* @author 树昌  
* @date 2019年1月25日
 */
@Service
public class OfficeService {

	@Autowired
	OfficeMapper officeMapper;
	
	
	/**
	 * 
	 * <p>Title: addOffice</p>  
	 * <p>Description:添加一条办公室信息的记录 </p>  
	 * @author 树昌
	 * @param office
	 * @return
	 */
	public boolean addOffice(Office office) {
		int row = officeMapper.insertSelective(office);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: showOffice</p>  
	 * <p>Description:查询办公室的记录 </p>  
	 * @author 树昌
	 * @param keyword
	 * @param startRow
	 * @param pageSize
	 * @return
	 */
	public List<Office> showOffice(String keyword, Integer startRow, Integer pageSize){
		
		OfficeExample officeExample = new OfficeExample();
		Criteria criteria = officeExample.createCriteria();
		criteria.andEnableEqualTo((byte) 1);
		// 模糊查询
		if (keyword != null) {
			keyword = "%" + keyword + "%";
			criteria.andOfficeNameLike(keyword);
			Criteria criteria2 = officeExample.or();
			criteria2.andOfficeBossNameLike(keyword);
			Criteria criteria3 = officeExample.or();
			criteria3.andOfficeBossTelephoneLike(keyword);
		}
		// 分页查询
		if (startRow != null && pageSize != null) {
			if (((startRow - 1) * pageSize) >= 0 && pageSize >= 1) {
				officeExample.setStartRow((startRow - 1) * pageSize);
				officeExample.setPageSize(pageSize);
			} else {
				return null;
			}
		}
		// 没有条件则为全查询
		return officeMapper.selectByExample(officeExample);
	}
	
	
	/**
	 * 
	 * <p>Title: deleteOffice</p>  
	 * <p>Description:删除一条办公室信息的记录 </p>  
	 * @author 树昌
	 * @param office
	 * @return
	 */
	public boolean deleteOffice(Office office) {
		
		OfficeExample officeExample =new OfficeExample();
		Criteria criteria = officeExample.createCriteria();
		criteria.andOfficeAreaIdEqualTo(office.getOfficeAreaId());
		office.setEnable((byte) 0);
		int row = officeMapper.updateByExampleSelective(office, officeExample);
		return row==1?true:false;
	}
	
	
	
	/**
	 * 
	 * <p>Title: updateOffice</p>  
	 * <p>Description:修改一条办公室记录 </p>  
	 * @author 树昌
	 * @param office
	 * @return
	 */
	public boolean updateOffice(Office office) {
		
		OfficeExample officeExample = new OfficeExample();
		Criteria criteria = officeExample.createCriteria();
		criteria.andOfficeAreaIdEqualTo(office.getOfficeAreaId());	
		int row = officeMapper.updateByExampleSelective(office, officeExample);
		return row==1?true:false;
	}
	
}
